using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using AutoGear.BLL;
using System.IO;
using ECommerce.Library;
using ECommerce.Library.DataStructure;
using System.Collections.Generic;
using System.Text;
using log4net;
using Entity;
using eShop.Model;
using eShop.DataAccess;
using eShop.Business;

public partial class admin_product_category_add : SiteAdminBasePage
{
    private ECommerce.DAL.CustomCollection<PartnerCategories> partnerCategories
    {
        get
        {

            return HttpContext.Current.Session["_partnerCategory"] as ECommerce.DAL.CustomCollection<PartnerCategories>;
        }
        set
        {
            HttpContext.Current.Session["_partnerCategory"] = value;
        }
    }

    private ECommerce.DAL.CustomCollection<Manufacturer> brands
    {
        get
        {
            return HttpContext.Current.Session["_brands"] as ECommerce.DAL.CustomCollection<Manufacturer>;
        }
        set
        {
            HttpContext.Current.Session["_brands"] = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindControls();
            LoadPartners();
        }
        uccResponseMessage.Visible = false;
    }

    protected void BindControls()
    {
        LoadProductCategory();
    }
    private void LoadPartners()
    {
        brands = ManufacturerManager.Brands(true);
        //brands.Add(new TblProductBrand(-1, "Select", "", "", "", "", "", "", true, true, true, DateTime.Now, 0));
        //ECommerce.DAL.CustomCollection<PartnerCategories> partCat = new ECommerce.DAL.CustomCollection<PartnerCategories>();
        //partnerCategories = partCat;
        cbxPartners.DataSource = brands;
        cbxPartners.DataBind();

    }
    protected void LoadProductCategory()
    {
        ddlParent.Items.Clear();
        ReadWriteCustomCollection<KeyValuePair<string, string>> productCategoryTree = CategoryManager.GetSubCategoryTree(SiteUtility.SuperRootCategoryId);
        foreach (KeyValuePair<string, string> productCategory in productCategoryTree)
        {
            ddlParent.Items.Add(new ListItem(productCategory.Key, productCategory.Value));
        }
    }

    protected void Create_Click(object sender, EventArgs e)
    {
        if (Convert.ToInt32(ddlParent.SelectedItem.Value) == 3)
        {
            uccResponseMessage.SetFailMessage("No new category is allowed under '" + ddlParent.SelectedItem.Text.Replace("--","").ToString() + "'!");
            uccResponseMessage.Visible = true;
        }
        else
        {
            try
            {
                string logoTempFile = null;
                string categoryTitle = null;
                string bannerTempFile = null;
                if (string.IsNullOrEmpty(tbxCategoryName.Text))
                {
                    throw new ArgumentNullException("Category Name");
                }
                else
                {
                    categoryTitle =Server.HtmlEncode(tbxCategoryName.Text.Trim());
                }

                if (fluCategoryLogo.HasFile)
                {
                    logoTempFile = SiteUtility.TempFolder + fluCategoryLogo.FileName;
                    if (File.Exists(MapPath(logoTempFile)))
                        FileAccessUtility.DeleteFile(logoTempFile);

                    string logoContentType = fluCategoryLogo.PostedFile.ContentType.ToLower();
                    if (logoContentType == "image/jpg" || logoContentType == "image/gif" || logoContentType == "image/png" || logoContentType == "image/x-png"
                        || logoContentType == "image/jpeg" || logoContentType == "image/pjpeg" || logoContentType == "image/bmp")
                    {
                        fluCategoryLogo.SaveAs(MapPath(logoTempFile));
                    }
                    else
                    {
                        uccResponseMessage.SetFailMessage(AutoGearCommonMessage.ImageFormateNotSupported);
                        uccResponseMessage.Visible = true;
                    }
                }

                if (fluCategoryBanner.HasFile)
                {
                    bannerTempFile = SiteUtility.TempFolder + fluCategoryBanner.FileName;
                    if (File.Exists(MapPath(bannerTempFile)))
                        FileAccessUtility.DeleteFile(bannerTempFile);

                    string bannerContentType = fluCategoryBanner.PostedFile.ContentType.ToLower();
                    if (bannerContentType == "image/jpg" || bannerContentType == "image/gif" || bannerContentType == "image/png" || bannerContentType == "image/x-png"
                        || bannerContentType == "image/jpeg" || bannerContentType == "image/pjpeg" || bannerContentType == "image/bmp")
                    {
                        fluCategoryBanner.SaveAs(MapPath(bannerTempFile));
                    }
                    else
                    {
                        uccResponseMessage.SetFailMessage(AutoGearCommonMessage.ImageFormateNotSupported);
                        uccResponseMessage.Visible = true;
                    }
                }
                string categoryDescription = tbxCategoryDescription.Text;
                int parentCategoryId = Convert.ToInt32(ddlParent.SelectedItem.Value);
                string displayOrder = RegExUtil.IsNumber(tbxDisplayOrder.Text.Trim()) ? tbxDisplayOrder.Text.Trim() : string.Empty;
                Category newProductCategoryAdded = CategoryManager.Create(tbxCategoryName.Text, categoryTitle, categoryDescription, logoTempFile, displayOrder, cbxIsActive.Checked, false, parentCategoryId, "0", bannerTempFile);
                if (newProductCategoryAdded != null)
                {
                    SaveAllPartners(newProductCategoryAdded.CategoryId);
                    uccResponseMessage.SetSuccessMessage("Category " + newProductCategoryAdded.CategoryName + " has been created");
                    uccResponseMessage.Visible = true;
                    LoadProductCategory();
                    ClearControls();
                }
            }
            catch (InvalidOperationException ex)
            {
                uccResponseMessage.SetFailMessage(ex.Message);
                uccResponseMessage.Visible = true;
            }
            catch (Exception ex)
            {
                uccResponseMessage.SetFailMessage(AutoGearCommonMessage.CategoryCreateFailed);
                uccResponseMessage.Visible = true;
            }
        }

    }

    private void SaveAllPartners(int categoryId)
    {
        foreach (ListItem li in cbxPartners.Items)
        {
            if (li.Selected == true)
            {
                TblPartnerCategories partnerCategories = new TblPartnerCategories();
                partnerCategories.CategoryId = categoryId;
                partnerCategories.PartnerId = Convert.ToInt32(li.Value);
                partnerCategories.Save();
            }
        }
    }

    private void ClearControls()
    {
        tbxCategoryName.Text = string.Empty;
        tbxCategoryDescription.Text = string.Empty;
        ddlParent.SelectedIndex = 0;
        tbxDisplayOrder.Text = string.Empty;
        cbxIsActive.Checked = false;
    }


}

